package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.PotentialCustomerMapper;
import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.unit.DataUnit;

/**
 * 潜在客户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-26
 */
@Service
public class PotentialCustomerServiceImpl implements IPotentialCustomerService 
{
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;

    /**
     * 查询潜在客户
     * 
     * @param id 潜在客户ID
     * @return 潜在客户
     */
    @Override
    public PotentialCustomer selectPotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.selectPotentialCustomerById(id);
    }

    /**
     * 查询潜在客户列表
     * 
     * @param potentialCustomer 潜在客户
     * @return 潜在客户
     */
    @Override
    public List<PotentialCustomer> selectPotentialCustomerList(PotentialCustomer potentialCustomer) {

         if (!SecurityUtils.getSubject().hasRole("admin")) {
              if (SecurityUtils.getSubject().hasRole("salesman") || SecurityUtils.getSubject().hasRole("generalManager")) {
                   return potentialCustomerMapper.selectPotentialCustomerByFollowUpUser(ShiroUtils.getUserId());
              }
         }
         return potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
    }

    /**
     * 新增潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int insertPotentialCustomer(PotentialCustomer potentialCustomer)
    {
        potentialCustomer.setCreateTime(DateUtils.getNowDate());
         List<PotentialCustomer> potentialCustomers = potentialCustomerMapper.selectAllPotentialCustomer();
         List<String> phoneNum = new ArrayList<>();
         for (PotentialCustomer customer : potentialCustomers) {
              //获取到所有客户的电话号码
              phoneNum.add(customer.getPhone());
         }
          //先对传进来的号码进行非空判定,如果为空,则
         if (potentialCustomer.getPhone() !=null && !potentialCustomer.getPhone().trim().equals("")) {
              if (phoneNum.contains(potentialCustomer.getPhone())) {
                   //如果传进来的客户号码和库中已有号码有重复
                   //插入失败
                   return -1;
              }else{
                   potentialCustomerMapper.insertPotentialCustomer(potentialCustomer);
              }

         }
         return 1;


    }

    /**
     * 修改潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int updatePotentialCustomer(PotentialCustomer potentialCustomer)
    {
         return potentialCustomerMapper.updatePotentialCustomer(potentialCustomer);
    }

    /**
     * 删除潜在客户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerByIds(String ids)
    {
        return potentialCustomerMapper.deletePotentialCustomerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除潜在客户信息
     * 
     * @param id 潜在客户ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.deletePotentialCustomerById(id);
    }

     @Override
     public List<PotentialCustomer> selectPotentialCustomerByFollowUpUser(Long id, PotentialCustomer potentialCustomer) {
         //获取当前角色id
          Long userId = ShiroUtils.getUserId();
          //根据前端页面传入的跟进人查询相关数据,如果没有传入跟进人数据,而且当前角色id是admin,那么久查询所有
          List<PotentialCustomer> potentialCustomers = potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
          List<Long> flowerUserId = new ArrayList<>();
          for (PotentialCustomer customer : potentialCustomers) {
               flowerUserId.add(customer.getFollowUpUser());
          }
          if (flowerUserId.contains(userId)){
               //有跟进人的情况
               return potentialCustomerMapper.selectPotentialCustomerByFollowUpUser(id,potentialCustomer);
          }else {
               //没有跟进人而且userId是管理员
               if (userId ==1){
                    return potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
               }
               return Collections.EMPTY_LIST;
          }
     }

     @Override
     public int insertFollowUpRecordByCustomerId(PotentialCustomer potentialCustomer) {
          //判断传入的跟进时间和跟进信息是否为空
          Date FollowUpTime = potentialCustomer.getLastFollowUpTime();
          String info = potentialCustomer.getInfo();
          if (FollowUpTime == null && info ==null &&info.equals("")) {
               return -1;
          }
          potentialCustomerMapper.insertPotentialCustomerByCustomerId(potentialCustomer);
          potentialCustomerMapper.updatePotentialCustomerByCustomerId(potentialCustomer);
          return 1;

     }
}
